﻿Public Class frmPHIDE
    Dim validator As PropertyValidator = New PropertyValidator
    'Dim sqlManager As New SQLManager
    'Dim sqlList As New SQLList
    Dim payrollManger As New PayrollManager
    Private Sub frmPHIDE_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ClearAllFields()
        If Clipboard.GetData(DataFormats.Text) <> "" Then
            Try
                'Dim phiList As List(Of PhilHealth) = sqlList.getItemPHI(Clipboard.GetData(DataFormats.Text))
                Dim phiList As List(Of PhilHealth) = payrollManger.getItemPHI(Clipboard.GetData(DataFormats.Text))
                For Each phi As PhilHealth In phiList
                    txtBraket.Text = phi.bracket
                    txtMinimumSalary.Text = phi.min_salary
                    txtMaximumSalary.Text = phi.max_salary
                    txtSalaryBase.Text = phi.base_salary
                    txtEmployerShare.Text = phi.employer_share
                    txtEmployeeShare.Text = phi.employee_share
                Next
                btnSave.Text = "&Update"
                btnClear.Text = "&Cancel"
            Catch ex As Exception
                MsgBox("Can't load Web page" & vbCrLf & ex.Message)
            End Try
        End If
    End Sub
    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If validator.IsEmptyString(txtBraket, "Braket") Then Return
        If validator.IsNumericString(txtMinimumSalary, "Minimum Salary") Then Return
        If validator.IsNumericString(txtMaximumSalary, "Maximum Salary") Then Return
        If validator.IsNumericString(txtSalaryBase, "Salary Base") Then Return
        If validator.IsNumericString(txtEmployerShare, "Employee Share") Then Return
        If validator.IsNumericString(txtEmployeeShare, "Employer Share") Then Return

        Try
            Dim philObject As New PhilHealth
            philObject.bracket = txtBraket.Text
            philObject.min_salary = txtMinimumSalary.Text
            philObject.max_salary = txtMaximumSalary.Text
            philObject.base_salary = txtSalaryBase.Text
            philObject.employer_share = txtEmployerShare.Text
            philObject.employee_share = txtEmployeeShare.Text

            If Clipboard.GetData(DataFormats.Text) = "" Then
                'sqlManager.executeQuery("INSERT INTO tbl_philhealth (bracket,min_salary, max_salary, base_salary, employer_share, employee_share) VALUE (" + txtBraket.Text + "," + txtMinimumSalary.Text + "," + txtMaximumSalary.Text + "," + txtSalaryBase.Text + "," + txtEmployerShare.Text + "," + txtEmployeeShare.Text + ")")


                payrollManger.insertItemPHI(philObject)
                MsgBox("Successfully saved.")
            Else
                'sqlManager.executeQuery("UPDATE tbl_philhealth SET bracket=" + txtBraket.Text + ",min_salary = " + txtMinimumSalary.Text + ", max_salary = " + txtMaximumSalary.Text + ", base_salary =" + txtSalaryBase.Text + " , employer_share =" + txtEmployerShare.Text + " , employee_share = " + txtEmployeeShare.Text + " WHERE id = " + Clipboard.GetData(DataFormats.Text) + "")
                payrollManger.updateItemPHI(txtBraket.Text, txtMinimumSalary.Text, txtMaximumSalary.Text, txtSalaryBase.Text, txtEmployerShare.Text, txtEmployeeShare.Text, Clipboard.GetData(DataFormats.Text))
                MsgBox("Successfully updated.")
            End If
            ClearAllFields()
        Catch ex As Exception
            MsgBox("Can't load Web page" & vbCrLf & ex.Message)
        End Try
        Me.Close()
    End Sub

    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        If Clipboard.GetData(DataFormats.Text) = "" Then
            validator.ResetControls(grpPHI)
        Else
            Me.Close()
        End If
    End Sub
    Public Function ClearAllFields()
        validator.ResetControls(grpPHI)
        btnSave.Text = "&Save"
        btnClear.Text = "&Clear"
        Return 0
    End Function
End Class